var cssList = {};
cssList.z_treegrid=$('#z_treegrid');// 主页面的 treegrid（DOM）
cssList.z_win=$("#win");//弹出窗口
cssList.z_form=$("#z_form");

$(function(){
    cssList.init();
    cssList.bindFunction();
    
});

cssList.init = function(){
	cssList.initTreegrid();
	cssList.initModal();//初始化窗口
}

cssList.initTreegrid = function(){
    cssList.z_treegrid.treegrid({    
        url:basePath+"/cssFile/queryFirst",   
        idField:'id',
        rownumbers: true,
     //   pagination: true,
        fit:true,
        treeField:'name',
        toolbar: '#z_treegrid_tb',
        fitColumns:true,
        columns:[[    
            {title:'名称',field:'name',width:100,align:'left'},    
            {title:'排序',field:'orderId',width:100,align:'center'} , 
            {title:'创建日期',field:'operTime',width:100,align:'center'} ,
            {title:'备注',field:'remark',width:100,align:'center'} ,
            {title:'操作',field:'serverName',width:100,align:'center',
                  formatter : function(value, row, index) {
                            if(value !=null && value!=""){
                                var s="<a target=\"_blank\" href='"+"\\zip\\zip\\cssUpload"+value;
                                   s+="'>";
                                   s+= "下载";
                                   s+="</a>";
                                    return s;   
                            }
                       }
            }    
        ]],
        onBeforeExpand:function(row){
            cssList.z_treegrid.treegrid("options").url = basePath+"/cssFile/queryNext?parentId="+row.id;  
            return true;      
        }  
    });  
};

/**
 *  easyui  按钮绑定事件
 */
cssList.bindFunction = function(){
    
    // 主页面按钮
    $("#z_add_btn").on("click",function(){
        cssList.add();
    });
    $("#z_update_btn").on("click",function(){
        cssList.update();
    });
    $("#z_delete_btn").on ("click",function(){
        cssList.remove();  
    });

};

cssList.initModal=function(){
    cssList.z_win.dialog({
        width:530,    
        height:430,    
        modal:true,
        draggable:false,//不可拖动
        shadow:true,//阴影
        collapsible:false,//不可折叠
        closed:true,
        onBeforeOpen:function(){
           cssList.z_form.form('reset');
           cssList.z_form.form('clear');
        },
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:function(){
                cssList.save();
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
                cssList.z_win.dialog('close');
                cssList.z_form.form('reset');
            }
        }]
    });  
};

//增加方法
cssList.add=function(){
    var selectNode=cssList.getSelected();
    if(selectNode){
    	if(selectNode.id==0){
            cssList.z_win.window('setTitle',"添加文件夹");
            cssList.z_win.dialog('open');
            cssList.hideLabel();
    	}else if(selectNode.modFlag==0){
    		 cssList.z_win.window('setTitle',"添加文件");
             cssList.z_win.dialog('open');
            cssList.showLabel();
    	}else{
    	  $.messager.alert('提示','<h4>请选择项目文件夹！</h4>');
    	}
    }else{
      $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    }
}

cssList.getSelected = function(){
    return  cssList.z_treegrid.treegrid('getSelected');
};

cssList.save=function(){
    var selectNode = cssList.getSelected();
    if(selectNode.id==0){//id=0新建文件夹
    	if(cssList.z_win.panel('options').title=='添加文件夹'){
    	   cssList.z_form.form('submit', {    
            url:basePath+"/cssFile/insertCssFolder",     
            onSubmit: function(param){    
              if(!cssList.z_form.form('validate')){
                return false;  
              }   
              param.parentId=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    cssList.z_win.window('close');
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    cssList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
    	}
    }else{
    	if(cssList.z_win.panel('options').title=='添加文件'){
            cssList.z_form.form('submit', {    
            url:basePath+"/cssFile/insertCssFile",     
            onSubmit: function(param){    
              if(!cssList.z_form.form('validate')){
                return false;  
              }   
              param.parentId=selectNode.id;
              var file=$("#fileWin_upload").val();
              if(file==null || file==""){
                $.messager.alert('提示','<h4>请上传文件！</h4>');
                return false;
              }
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    cssList.z_win.window('close');
                     cssList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
        if(cssList.z_win.panel('options').title=='修改文件夹'){
            cssList.z_form.form('submit', {    
            url:basePath+"/cssFile/updateCssFolder",     
            onSubmit: function(param){    
              if(!cssList.z_form.form('validate')){
                return false;  
              }   
              param.id=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    cssList.z_win.window('close');
                     cssList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
        if(cssList.z_win.panel('options').title=='修改文件备注'){
           cssList.z_form.form('submit', {    
            url:basePath+"/cssFile/updateCssFile",     
            onSubmit: function(param){    
              if(!cssList.z_form.form('validate')){
                return false;  
              }   
              param.id=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    cssList.z_win.window('close');
                     cssList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
    }
    
};

cssList.update=function(){
    var selectNode=cssList.getSelected();
    if(selectNode){
    	if(selectNode.id==0){
    		 $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    	}else{
           if(selectNode.modFlag==0){
            cssList.z_win.window('setTitle',"修改文件夹");
            cssList.z_win.dialog('open');
            cssList.z_form.form('load',selectNode);
            cssList.hideLabel();
            }else{
                cssList.z_win.window('setTitle',"修改文件备注");
                cssList.z_win.dialog('open');
                cssList.z_form.form('load',selectNode);
                $("#fileLabel").hide();
                $('#fileLabel').attr("disabled",true);
            }    	
    	}
    }else{
      $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    }
}

cssList.hideLabel=function(){
    $("#remarkLabel").hide();
    $('#remarkLabel').attr("disabled",true);
    $("#fileLabel").hide();
    $('#fileLabel').attr("disabled",true);
   // $('#fileWin_upload').textbox({disabled:true});
    $('#fileWin_upload').attr("disabled",true);
}

cssList.showLabel=function(){
    $("#remarkLabel").show();
    $("#fileLabel").show(); 
    $('#remarkLabel').attr("disabled",false);
    $('#fileLabel').attr("disabled",false);
   // $('#fileWin_upload').textbox({disabled:false});
    $('#fileWin_upload').attr("disabled",false);
}

cssList.remove=function(){
   var selectNode = cssList.getSelected();
    if(selectNode && selectNode.modFlag==1){
        $.messager.confirm('确认','<h4>您确认想要删除['+selectNode.name+']吗？</h4>',function(r){    
            if (r){    
                 $.ajax({
                    url: base.path("/cssFile/deleteCssFile"),
                    type: "post",
                    data:{"id":selectNode.id},
                    async:false,
                    success: function (data) {
                        if(data == 1){
                            $.messager.alert('提示','<h4>删除成功！</h4>');
                              cssList.initTreegrid();
                            return;
                        }else{
                            $.messager.alert('提示','<h4>删除失败，网络异常！</h4>');
                            return;
                        }
                    }
                });
            }    
        });  
        
    }else{
        $.messager.alert('提示','<h4>请选择一个项目！</h4>');
        return;
    }
}